***********************************************************************************************************************************************************************************;
*Program: Additional File 15                                                                                                                                                       ;                                                               
*Purpose: BIC backward elimination for individual outcomes for Scenario 1 simulated uncorrelated data. Obtain Union model                                                          ;                                     
*Statisticians: Grisell Diaz-Ramirez and Siqi Gan   																   ;
*Finished: 2020.04.02																				   ;
***********************************************************************************************************************************************************************************;

/*Check system options specified at SAS invocation*/
proc options option=work; run;
proc options option=utilloc; run;
proc options group=(memory performance); run;

libname savedata "path";
options nomlogic nomprint nomrecall nosymbolgen;
options nosource nonotes; /*nosource: suppress the listing of the SAS statements to the log, causes only source lines that contain errors to be written to the log*/
options noquotelenmax; /*supress warning: THE QUOTED STRING CURRENTLY BEING PROCESSED HAS BECOME MORE THAN 262 CHARACTERS LONG*/
ods select none; /*to create output data sets through the ODS OUTPUT statement and suppress the display of all output*/


*****************************************************PREPARE DATA FOR MACROs ********************************************************;

proc sort data=savedata.sim500uncorr out=simdata; by newid; run; 

*Merge with original dataset to get the covariates;
data simdata2; 
  merge savedata.originaldata (drop=time_adldepdth status_adldepdth time_iadldifdth status_iadldifdth time_walkdepdth status_walkdepdth time2death death) 
        simdata;
  by newid;
proc sort; by sim outcome newid; run;
/*5531*SIM=500->2,765,500*4outcomes=11,062,000 observations and 56+6-1=61 variables.*/

*ADL;
data simdata3;
 set simdata2 (rename=(time=time_adldepdth status=status_adldepdth));
 where outcome="adl";
proc sort; by sim newid; run; /*2,765,500 observations and 60 variables*/

proc delete data=simdata simdata2; run; quit;


**************************************************** DEFINE ARGUMENTS FOR MACROS: outcome, best_bic, DeleteOneVar, cstat ********************************************************;

*Note: all the macro variables for outcomes need to be in the same order, e.g. : adl iadl walk death; 
%let DATA=simdata4;
%let NUMOUTCOMES=1; /*number of outcomes*/
%let ALLOUTCOME=status_adldepdth;
%let ALLTIME=time_adldepdth;
%let ALLLABEL= adl; /*labels for outcomes*/
%let NUMPRED=37; /*number of predictors without including predictors that are forced in (e.g. dAGE, SEX*/
%let NUMPREDPLUSONE=38; /*number of predictors defined in &NUMPRED (e.g.37) plus one*/
proc sql noprint; select max(sim) format 3. into :S from simdata3; quit; /*create macro variable with total number of simulated datasets*/
%put "&S"; 

***************************************************** BACKWARD ELIMINATION 4 INDIVIDUAL OUTCOMES: ADL **************************************************************;
/*Macro outcome, for each simulated dataset and each outcome: 
1-) Calls macro BEST_BIC (calls macro DeleteOneVar)
2-) Creates one dataset with results from backward elimination
3-) Create a dataset with one line uncorresponding to the best model for each individual outcome and each simulation
*/

%macro outcome;
 %do i=1 %to &S; /*do this for each simulated dataset up to the last simulated dataset*/
  proc sql noprint; select (&i-1)*(nobs/&S)+1 into :fobs from dictionary.tables where libname='WORK' and memname='SIMDATA3'; quit; /*create macro variable with the first id of ith simulation*/
  proc sql noprint; select &i*(nobs/&S) into :lobs from dictionary.tables where libname='WORK' and memname='SIMDATA3'; quit; /*create macro variable with the last id of ith simulation*/

  data simdata4;
   set simdata3 (FIRSTOBS=&fobs OBS=&lobs);
  run;

  sasfile WORK.simdata4 load;

  %PUT "&i" ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;
  %do j=1 %to &NUMOUTCOMES;
    %let OUTCOME=%scan(&ALLOUTCOME,&j); /*extract the ith outcome, ith time, ith label*/
    %let TIME=%scan(&ALLTIME,&j);
	%let LABEL=%scan(&ALLLABEL,&j);
  
	%best_bic;

    sasfile WORK.BIC_&label close;

	/*Create new BIC_&label dataset to merge with other outcomes*/
	data BIC_&label (rename=(VARINMODEL=VARINMODEL_&label)); 
     set BIC_&label (drop=DELELIST DELEVAR);
     sim=&i;
    run;

	/*For each outcome and each simulation: select the best individual model*/
	proc sort data=BIC_&label; by descending BIC_&label; 
    data BIC_&label;
     set BIC_&label point=nobs nobs=nobs; 
	 output;
	 stop;
    run;

  %end; /*j loop*/

  sasfile WORK.simdata4 close;
  proc delete data=simdata4; run; quit;

  proc append base=BICsim_&label data=BIC_&label force; run; /*BICsim will have the information from all best individual models for each outcome and for each simulated dataset*/
  proc delete data=BIC_&label; run; quit;
  %PUT "&i" ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;

 %end; /*i loop*/
%mend outcome;

%macro best_bic;
	%let BASE=%sysfunc(compbl(dAGE SEX ALCOHOL ARTHRITIS CANCER COGDLRC3G COGIMRC3G DIABETES DRIVE EDU EXERCISE EYE2G
			                                   FALL HEARAID HEARING HEARTFAILURE HYPERTENSION INCONTINENCE LALONE LUNG MSTAT OTHERARM
	                                           OTHERCHAIR OTHERCLIM3G OTHERDIME OTHERLIFT OTHERPUSH OTHERSIT OTHERSTOOP OTHERWALK PAIN CESDALL
			                                   qBMI qFAGE qMAGE SHLT SMOKING STROKE VOLUNTEER)); /*all 39 initial variables*/
	%let DELE=%sysfunc(compbl(ALCOHOL ARTHRITIS CANCER COGDLRC3G COGIMRC3G DIABETES DRIVE EDU EXERCISE EYE2G
			                                   FALL HEARAID HEARING HEARTFAILURE HYPERTENSION INCONTINENCE LALONE LUNG MSTAT OTHERARM
	                                           OTHERCHAIR OTHERCLIM3G OTHERDIME OTHERLIFT OTHERPUSH OTHERSIT OTHERSTOOP OTHERWALK PAIN CESDALL
			                                   qBMI qFAGE qMAGE SHLT SMOKING STROKE VOLUNTEER)); /*all 39 variables-2(AGECAT SEX)=37*/

    proc phreg data = &DATA CONCORDANCE=HARRELL rocoptions(method=RECURSIVE iauc); 
      class &BASE;
      model &time*&outcome(0) = &BASE;
	  ods output FITSTATISTICS=FITS1 CONCORDANCE=concord IAUC=iauc;
    run;

	data _null_; set concord; call symputx ('c' ,Estimate); run;
	data _null_; set iauc; call symputx ('iauc' ,Estimate); run;

	data base_&label (keep=VARINMODEL DELEVAR DELELIST AIC_&label BIC_&label C_&label iauc_&label); 
      length VARINMODEL $ 600 DELELIST $600 DELEVAR $ 60;
      set FITS1 end=last;
      retain VARINMODEL DELEVAR C_&label iauc_&label AIC_&label BIC_&label; 
      format AIC_&label BIC_&label 10.4; 
	  VARINMODEL="&BASE";
	  DELEVAR=" ";
	  DELELIST=" "; 
	  C_&label=&c;
	  iauc_&label=&iauc;
	  if CRITERION='AIC' then AIC_&label=WITHCOVARIATES;
	  if CRITERION='SBC' then BIC_&label=WITHCOVARIATES;
      if last;
     run;
	
    proc delete data=FITS1 concord iauc; run; quit;

   proc append base=BIC_&label data=base_&label force; run; /*BIC is the final dataset with backward elimination steps from 39 variables to 2*/
   sasfile WORK.BIC_&label load;
   proc delete data=base_&label; run; quit;

	%do k=1 %to &NUMPRED;
	  %DeleteOneVar;
      data _null_;
       set CTABLE_&label (keep=VARINMODEL DELELIST);
       call symputx ('BASE' ,VARINMODEL);
	   call symputx ('DELE' ,DELELIST);
      run;

      proc append base=BIC_&label data=CTABLE_&label force; run;
	  proc delete data=CTABLE_&label; run; quit;
	%end;
%mend best_bic;

%macro DeleteOneVar;
 	%do l=1 %to %eval(&NUMPREDPLUSONE-&k);
     %let DELEVAR=%scan(&DELE,&l); /*select the lth word to delete. &DELE is defined in 'best_bic' macro*/
	 %let VARNAME=%sysfunc(compbl(%sysfunc(tranwrd(&BASE,&DELEVAR,%str( ))))); /*select the final set of variables to run model by replacing the deleted variable with blank*/

     proc phreg data = &DATA CONCORDANCE=HARRELL rocoptions(method=RECURSIVE iauc); 
      class &VARNAME;
      model &time*&outcome(0) = &VARNAME;
	  ods output FITSTATISTICS=FITS1 CONCORDANCE=concord IAUC=iauc;
     run;

	data _null_; set concord; call symputx ('c' ,Estimate); run;
	data _null_; set iauc; call symputx ('iauc' ,Estimate); run;

	data FITS2 (keep=VARINMODEL DELEVAR DELELIST AIC_&label BIC_&label C_&label iauc_&label); 
      length VARINMODEL $ 600 DELELIST $600 DELEVAR $ 60;
      set FITS1 end=last;
      retain VARINMODEL DELEVAR C_&label iauc_&label AIC_&label BIC_&label; 
      format AIC_&label BIC_&label 10.4; 
	  VARINMODEL="&VARNAME"; /*variables in the model*/
	  DELEVAR="&DELEVAR"; /*deleted variable*/
	  DELELIST=compbl(tranwrd("&DELE","&DELEVAR",' ')); /*'DELELIST' contain the list of variables that we need to start with in subsequent run*/
	  C_&label=&c;
	  iauc_&label=&iauc;
	  if CRITERION='AIC' then AIC_&label=WITHCOVARIATES;
	  if CRITERION='SBC' then BIC_&label=WITHCOVARIATES;
      if last;
     run;

    %if &l=1 %then %do; /*in the 1st line of CTABLE_&label create CTABLE_&label dataset*/
     proc append base=CTABLE_&label data=FITS2 force; run;
     sasfile WORK.CTABLE_&label load;
    %end;
    %else %do; /*for the rest of lines of CTABLE_&label: keep updating CTABLE_&label dataset in memory*/
     proc append base=CTABLE_&label data=FITS2 force; run;
    %end; 
    proc delete data=FITS1 FITS2 concord iauc; run; quit;
	/*proc delete data=FITS1 FITS2 CensUncens DF concord iauc; run; quit;*/
  %end;
  sasfile WORK.CTABLE_&label close;

  proc sort data=CTABLE_&label; by descending BIC_&label; run;
  data CTABLE_&label;
   set CTABLE_&label point=nobs nobs=nobs;
   output;
   stop;
  run; /*choose the last observation with minimum BIC*/
%mend DeleteOneVar;

%PUT ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;
%outcome;
%PUT ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;

*Create permanent dataset;
%let LABEL= adl; /*labels for outcomes*/
data savedata.BICsim_uncorr_&label; set BICsim_&label; proc sort; by sim; run;
proc delete data=BICsim_&label; run; quit;
***************************************************** END BACKWARD ELIMINATION 4 INDIVIDUAL OUTCOMES: ADL ******************************************************************;


*****************************************************PREPARE DATA FOR MACROs ********************************************************;
proc sort data=savedata.sim500uncorr out=simdata; by newid; run; 

*Merge with original dataset to get the covariates;
data simdata2; 
  merge savedata.originaldata (drop=time_adldepdth status_adldepdth time_iadldifdth status_iadldifdth time_walkdepdth status_walkdepdth time2death death) 
        simdata;
  by newid;
proc sort; by sim outcome newid; run;
/*5531*SIM=500->2,765,500*4outcomes=11,062,000 observations and 56+6-1=61 variables.*/

*IADL;
data simdata3;
 set simdata2 (rename=(time=time_iadldifdth status=status_iadldifdth));
 where outcome="iadl";
proc sort; by sim newid; run; /*2,765,500 observations and 60 variables*/

proc delete data=simdata simdata2; run; quit;


**************************************************** DEFINE ARGUMENTS FOR MACROS: outcome, best_bic, DeleteOneVar, cstat ********************************************************;

*Note: all the macro variables for outcomes need to be in the same order, e.g. : adl iadl walk death; 
%let DATA=simdata4;
%let NUMOUTCOMES=1; /*number of outcomes*/
%let ALLOUTCOME=status_iadldifdth;
%let ALLTIME=time_iadldifdth;
%let ALLLABEL= iadl; /*labels for outcomes*/
%let NUMPRED=37; /*number of predictors without including predictors that are forced in (e.g. dAGE, SEX*/
%let NUMPREDPLUSONE=38; /*number of predictors defined in &NUMPRED (e.g.37) plus one*/
proc sql noprint; select max(sim) format 3. into :S from simdata3; quit; /*create macro variable with total number of simulated datasets*/
%put "&S"; 

***************************************************** BACKWARD ELIMINATION 4 INDIVIDUAL OUTCOMES: IADL **************************************************************;
/*Macro outcome, for each simulated dataset and each outcome: 
1-) Calls macro BEST_BIC (calls macro DeleteOneVar)
2-) Creates one dataset with results from backward elimination
3-) Create a dataset with one line uncorresponding to the best model for each individual outcome and each simulation
*/

%macro outcome;
 %do i=1 %to &S; /*do this for each simulated dataset up to the last simulated dataset*/
  proc sql noprint; select (&i-1)*(nobs/&S)+1 into :fobs from dictionary.tables where libname='WORK' and memname='SIMDATA3'; quit; /*create macro variable with the first id of ith simulation*/
  proc sql noprint; select &i*(nobs/&S) into :lobs from dictionary.tables where libname='WORK' and memname='SIMDATA3'; quit; /*create macro variable with the last id of ith simulation*/

  data simdata4;
   set simdata3 (FIRSTOBS=&fobs OBS=&lobs);
  run;

  sasfile WORK.simdata4 load;

  %PUT "&i" ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;
  %do j=1 %to &NUMOUTCOMES;
    %let OUTCOME=%scan(&ALLOUTCOME,&j); /*extract the ith outcome, ith time, ith label*/
    %let TIME=%scan(&ALLTIME,&j);
	%let LABEL=%scan(&ALLLABEL,&j);
  
	%best_bic;

    sasfile WORK.BIC_&label close;

	/*Create new BIC_&label dataset to merge with other outcomes*/
	data BIC_&label (rename=(VARINMODEL=VARINMODEL_&label)); 
     set BIC_&label (drop=DELELIST DELEVAR);
     sim=&i;
    run;

	/*For each outcome and each simulation: select the best individual model*/
	proc sort data=BIC_&label; by descending BIC_&label; 
    data BIC_&label;
     set BIC_&label point=nobs nobs=nobs; 
	 output;
	 stop;
    run;

  %end; /*j loop*/

  sasfile WORK.simdata4 close;
  proc delete data=simdata4; run; quit;

  proc append base=BICsim_&label data=BIC_&label force; run; /*BICsim will have the information from all best individual models for each outcome and for each simulated dataset*/
  proc delete data=BIC_&label; run; quit;
  %PUT "&i" ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;

 %end; /*i loop*/
%mend outcome;

%macro best_bic;
	%let BASE=%sysfunc(compbl(dAGE SEX ALCOHOL ARTHRITIS CANCER COGDLRC3G COGIMRC3G DIABETES DRIVE EDU EXERCISE EYE2G
			                                   FALL HEARAID HEARING HEARTFAILURE HYPERTENSION INCONTINENCE LALONE LUNG MSTAT OTHERARM
	                                           OTHERCHAIR OTHERCLIM3G OTHERDIME OTHERLIFT OTHERPUSH OTHERSIT OTHERSTOOP OTHERWALK PAIN CESDALL
			                                   qBMI qFAGE qMAGE SHLT SMOKING STROKE VOLUNTEER)); /*all 39 initial variables*/
	%let DELE=%sysfunc(compbl(ALCOHOL ARTHRITIS CANCER COGDLRC3G COGIMRC3G DIABETES DRIVE EDU EXERCISE EYE2G
			                                   FALL HEARAID HEARING HEARTFAILURE HYPERTENSION INCONTINENCE LALONE LUNG MSTAT OTHERARM
	                                           OTHERCHAIR OTHERCLIM3G OTHERDIME OTHERLIFT OTHERPUSH OTHERSIT OTHERSTOOP OTHERWALK PAIN CESDALL
			                                   qBMI qFAGE qMAGE SHLT SMOKING STROKE VOLUNTEER)); /*all 39 variables-2(AGECAT SEX)=37*/

    proc phreg data = &DATA CONCORDANCE=HARRELL rocoptions(method=RECURSIVE iauc); 
      class &BASE;
      model &time*&outcome(0) = &BASE;
	  ods output FITSTATISTICS=FITS1 CONCORDANCE=concord IAUC=iauc;
    run;

	data _null_; set concord; call symputx ('c' ,Estimate); run;
	data _null_; set iauc; call symputx ('iauc' ,Estimate); run;

	data base_&label (keep=VARINMODEL DELEVAR DELELIST AIC_&label BIC_&label C_&label iauc_&label); 
      length VARINMODEL $ 600 DELELIST $600 DELEVAR $ 60;
      set FITS1 end=last;
      retain VARINMODEL DELEVAR C_&label iauc_&label AIC_&label BIC_&label; 
      format AIC_&label BIC_&label 10.4; 
	  VARINMODEL="&BASE";
	  DELEVAR=" ";
	  DELELIST=" "; 
	  C_&label=&c;
	  iauc_&label=&iauc;
	  if CRITERION='AIC' then AIC_&label=WITHCOVARIATES;
	  if CRITERION='SBC' then BIC_&label=WITHCOVARIATES;
      if last;
     run;
	
    proc delete data=FITS1 concord iauc; run; quit;

   proc append base=BIC_&label data=base_&label force; run; /*BIC is the final dataset with backward elimination steps from 39 variables to 2*/
   sasfile WORK.BIC_&label load;
   proc delete data=base_&label; run; quit;

	%do k=1 %to &NUMPRED;
	  %DeleteOneVar;
      data _null_;
       set CTABLE_&label (keep=VARINMODEL DELELIST);
       call symputx ('BASE' ,VARINMODEL);
	   call symputx ('DELE' ,DELELIST);
      run;

      proc append base=BIC_&label data=CTABLE_&label force; run;
	  proc delete data=CTABLE_&label; run; quit;
	%end;
%mend best_bic;

%macro DeleteOneVar;
 	%do l=1 %to %eval(&NUMPREDPLUSONE-&k);
     %let DELEVAR=%scan(&DELE,&l); /*select the lth word to delete. &DELE is defined in 'best_bic' macro*/
	 %let VARNAME=%sysfunc(compbl(%sysfunc(tranwrd(&BASE,&DELEVAR,%str( ))))); /*select the final set of variables to run model by replacing the deleted variable with blank*/

     proc phreg data = &DATA CONCORDANCE=HARRELL rocoptions(method=RECURSIVE iauc); 
      class &VARNAME;
      model &time*&outcome(0) = &VARNAME;
	  ods output FITSTATISTICS=FITS1 CONCORDANCE=concord IAUC=iauc;
     run;

	data _null_; set concord; call symputx ('c' ,Estimate); run;
	data _null_; set iauc; call symputx ('iauc' ,Estimate); run;

	data FITS2 (keep=VARINMODEL DELEVAR DELELIST AIC_&label BIC_&label C_&label iauc_&label); 
      length VARINMODEL $ 600 DELELIST $600 DELEVAR $ 60;
      set FITS1 end=last;
      retain VARINMODEL DELEVAR C_&label iauc_&label AIC_&label BIC_&label; 
      format AIC_&label BIC_&label 10.4; 
	  VARINMODEL="&VARNAME"; /*variables in the model*/
	  DELEVAR="&DELEVAR"; /*deleted variable*/
	  DELELIST=compbl(tranwrd("&DELE","&DELEVAR",' ')); /*'DELELIST' contain the list of variables that we need to start with in subsequent run.*/
	  C_&label=&c;
	  iauc_&label=&iauc;
	  if CRITERION='AIC' then AIC_&label=WITHCOVARIATES;
	  if CRITERION='SBC' then BIC_&label=WITHCOVARIATES;
      if last;
     run;

    %if &l=1 %then %do; /*in the 1st line of CTABLE_&label create CTABLE_&label dataset*/
     proc append base=CTABLE_&label data=FITS2 force; run;
     sasfile WORK.CTABLE_&label load;
    %end;
    %else %do; /*for the rest of lines of CTABLE_&label: keep updating CTABLE_&label dataset in memory*/
     proc append base=CTABLE_&label data=FITS2 force; run;
    %end; 
    proc delete data=FITS1 FITS2 concord iauc; run; quit;
	/*proc delete data=FITS1 FITS2 CensUncens DF concord iauc; run; quit;*/
  %end;
  sasfile WORK.CTABLE_&label close;

  proc sort data=CTABLE_&label; by descending BIC_&label; run;
  data CTABLE_&label;
   set CTABLE_&label point=nobs nobs=nobs;
   output;
   stop;
  run; /*choose the last observation with minimum BIC*/
%mend DeleteOneVar;

%PUT ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;
%outcome;
%PUT ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;

*Create permanent dataset;
%let LABEL= iadl; /*labels for outcomes*/
data savedata.BICsim_uncorr_&label; set BICsim_&label; proc sort; by sim; run;
proc delete data=BICsim_&label; run; quit;
***************************************************** END BACKWARD ELIMINATION 4 INDIVIDUAL OUTCOMES: IADL ******************************************************************;


*****************************************************PREPARE DATA FOR MACROs ********************************************************;
proc sort data=savedata.sim500uncorr out=simdata; by newid; run; 

*Merge with original dataset to get the covariates;
data simdata2; 
  merge savedata.originaldata (drop=time_adldepdth status_adldepdth time_iadldifdth status_iadldifdth time_walkdepdth status_walkdepdth time2death death) 
        simdata;
  by newid;
proc sort; by sim outcome newid; run;
/*5531*SIM=500->2,765,500*4outcomes=11,062,000 observations and 56+6-1=61 variables.*/

*Walk;
data simdata3;
 set simdata2 (rename=(time=time_walkdepdth status=status_walkdepdth));
 where outcome="walk";
proc sort; by sim newid; run; /*2,765,500 observations and 60 variables*/

proc delete data=simdata simdata2; run; quit;


**************************************************** DEFINE ARGUMENTS FOR MACROS: outcome, best_bic, DeleteOneVar, cstat ********************************************************;

*Note: all the macro variables for outcomes need to be in the same order, e.g. : adl iadl walk death; 
%let DATA=simdata4;
%let NUMOUTCOMES=1; /*number of outcomes*/
%let ALLOUTCOME=status_walkdepdth;
%let ALLTIME=time_walkdepdth;
%let ALLLABEL= walk; /*labels for outcomes*/
%let NUMPRED=37; /*number of predictors without including predictors that are forced in (e.g. dAGE, SEX*/
%let NUMPREDPLUSONE=38; /*number of predictors defined in &NUMPRED (e.g.37) plus one*/
proc sql noprint; select max(sim) format 3. into :S from simdata3; quit; /*create macro variable with total number of simulated datasets*/
%put "&S"; 

***************************************************** BACKWARD ELIMINATION 4 INDIVIDUAL OUTCOMES: WALK **************************************************************;
/*Macro outcome, for each simulated dataset and each outcome: 
1-) Calls macro BEST_BIC (calls macro DeleteOneVar)
2-) Creates one dataset with results from backward elimination
3-) Create a dataset with one line uncorresponding to the best model for each individual outcome and each simulation
*/

%macro outcome;
 %do i=1 %to &S; /*do this for each simulated dataset up to the last simulated dataset*/
  proc sql noprint; select (&i-1)*(nobs/&S)+1 into :fobs from dictionary.tables where libname='WORK' and memname='SIMDATA3'; quit; /*create macro variable with the first id of ith simulation*/
  proc sql noprint; select &i*(nobs/&S) into :lobs from dictionary.tables where libname='WORK' and memname='SIMDATA3'; quit; /*create macro variable with the last id of ith simulation*/

  data simdata4;
   set simdata3 (FIRSTOBS=&fobs OBS=&lobs);
  run;

  sasfile WORK.simdata4 load;

  %PUT "&i" ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;
  %do j=1 %to &NUMOUTCOMES;
    %let OUTCOME=%scan(&ALLOUTCOME,&j); /*extract the ith outcome, ith time, ith label*/
    %let TIME=%scan(&ALLTIME,&j);
	%let LABEL=%scan(&ALLLABEL,&j);
  
	%best_bic;

    sasfile WORK.BIC_&label close;

	/*Create new BIC_&label dataset to merge with other outcomes*/
	data BIC_&label (rename=(VARINMODEL=VARINMODEL_&label ); 
     set BIC_&label (drop=DELELIST DELEVAR);
     sim=&i;
    run;

	/*For each outcome and each simulation: select the best individual model*/
	proc sort data=BIC_&label; by descending BIC_&label; 
    data BIC_&label;
     set BIC_&label point=nobs nobs=nobs; 
	 output;
	 stop;
    run;

  %end; /*j loop*/

  sasfile WORK.simdata4 close;
  proc delete data=simdata4; run; quit;

  proc append base=BICsim_&label data=BIC_&label force; run; /*BICsim will have the information from all best individual models for each outcome and for each simulated dataset*/
  proc delete data=BIC_&label; run; quit;
  %PUT "&i" ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;

 %end; /*i loop*/
%mend outcome;

%macro best_bic;
	%let BASE=%sysfunc(compbl(dAGE SEX ALCOHOL ARTHRITIS CANCER COGDLRC3G COGIMRC3G DIABETES DRIVE EDU EXERCISE EYE2G
			                                   FALL HEARAID HEARING HEARTFAILURE HYPERTENSION INCONTINENCE LALONE LUNG MSTAT OTHERARM
	                                           OTHERCHAIR OTHERCLIM3G OTHERDIME OTHERLIFT OTHERPUSH OTHERSIT OTHERSTOOP OTHERWALK PAIN CESDALL
			                                   qBMI qFAGE qMAGE SHLT SMOKING STROKE VOLUNTEER)); /*all 39 initial variables*/
	%let DELE=%sysfunc(compbl(ALCOHOL ARTHRITIS CANCER COGDLRC3G COGIMRC3G DIABETES DRIVE EDU EXERCISE EYE2G
			                                   FALL HEARAID HEARING HEARTFAILURE HYPERTENSION INCONTINENCE LALONE LUNG MSTAT OTHERARM
	                                           OTHERCHAIR OTHERCLIM3G OTHERDIME OTHERLIFT OTHERPUSH OTHERSIT OTHERSTOOP OTHERWALK PAIN CESDALL
			                                   qBMI qFAGE qMAGE SHLT SMOKING STROKE VOLUNTEER)); /*all 39 variables-2(AGECAT SEX)=37*/

    proc phreg data = &DATA CONCORDANCE=HARRELL rocoptions(method=RECURSIVE iauc); 
      class &BASE;
      model &time*&outcome(0) = &BASE;
	  ods output FITSTATISTICS=FITS1 CONCORDANCE=concord IAUC=iauc;
    run;

	data _null_; set concord; call symputx ('c' ,Estimate); run;
	data _null_; set iauc; call symputx ('iauc' ,Estimate); run;

	data base_&label (keep=VARINMODEL DELEVAR DELELIST AIC_&label BIC_&label C_&label iauc_&label); 
      length VARINMODEL $ 600 DELELIST $600 DELEVAR $ 60;
      set FITS1 end=last;
      retain VARINMODEL DELEVAR C_&label iauc_&label AIC_&label BIC_&label; 
      format AIC_&label BIC_&label 10.4; 
	  VARINMODEL="&BASE";
	  DELEVAR=" ";
	  DELELIST=" "; 
	  C_&label=&c;
	  iauc_&label=&iauc;
	  if CRITERION='AIC' then AIC_&label=WITHCOVARIATES;
	  if CRITERION='SBC' then BIC_&label=WITHCOVARIATES;
      if last;
     run;
	
    proc delete data=FITS1 concord iauc; run; quit;

   proc append base=BIC_&label data=base_&label force; run; /*BIC is the final dataset with backward elimination steps from 39 variables to 2*/
   sasfile WORK.BIC_&label load;
   proc delete data=base_&label; run; quit;

	%do k=1 %to &NUMPRED;
	  %DeleteOneVar;
      data _null_;
       set CTABLE_&label (keep=VARINMODEL DELELIST );
       call symputx ('BASE' ,VARINMODEL);
	   call symputx ('DELE' ,DELELIST);
      run;

      proc append base=BIC_&label data=CTABLE_&label force; run;
	  proc delete data=CTABLE_&label; run; quit;
	%end;
%mend best_bic;

%macro DeleteOneVar;
 	%do l=1 %to %eval(&NUMPREDPLUSONE-&k);
     %let DELEVAR=%scan(&DELE,&l); /*select the lth word to delete. &DELE is defined in 'best_bic' macro*/
	 %let VARNAME=%sysfunc(compbl(%sysfunc(tranwrd(&BASE,&DELEVAR,%str( ))))); /*select the final set of variables to run model by replacing the deleted variable with blank*/

     proc phreg data = &DATA CONCORDANCE=HARRELL rocoptions(method=RECURSIVE iauc); 
      class &VARNAME;
      model &time*&outcome(0) = &VARNAME;
	  ods output FITSTATISTICS=FITS1 CONCORDANCE=concord IAUC=iauc;
     run;

	data _null_; set concord; call symputx ('c' ,Estimate); run;
	data _null_; set iauc; call symputx ('iauc' ,Estimate); run;

	data FITS2 (keep=VARINMODEL DELEVAR DELELIST  AIC_&label BIC_&label C_&label iauc_&label); 
      length VARINMODEL $ 600 DELELIST $600 DELEVAR $ 60;
      set FITS1 end=last;
      retain VARINMODEL DELEVAR C_&label iauc_&label AIC_&label BIC_&label; 
      format AIC_&label BIC_&label 10.4; 
	  VARINMODEL="&VARNAME"; /*variables in the model*/
	  DELEVAR="&DELEVAR"; /*deleted variable*/
	  DELELIST=compbl(tranwrd("&DELE","&DELEVAR",' ')); /*'DELELIST' contain the list of variables that we need to start with in subsequent run.*/
	  C_&label=&c;
	  iauc_&label=&iauc;
	  if CRITERION='AIC' then AIC_&label=WITHCOVARIATES;
	  if CRITERION='SBC' then BIC_&label=WITHCOVARIATES;
      if last;
     run;

    %if &l=1 %then %do; /*in the 1st line of CTABLE_&label create CTABLE_&label dataset*/
     proc append base=CTABLE_&label data=FITS2 force; run;
     sasfile WORK.CTABLE_&label load;
    %end;
    %else %do; /*for the rest of lines of CTABLE_&label: keep updating CTABLE_&label dataset in memory*/
     proc append base=CTABLE_&label data=FITS2 force; run;
    %end; 
    proc delete data=FITS1 FITS2 concord iauc; run; quit;
	/*proc delete data=FITS1 FITS2 CensUncens DF concord iauc; run; quit;*/
  %end;
  sasfile WORK.CTABLE_&label close;

  proc sort data=CTABLE_&label; by descending BIC_&label; run;
  data CTABLE_&label;
   set CTABLE_&label point=nobs nobs=nobs;
   output;
   stop;
  run; /*choose the last observation with minimum BIC*/
%mend DeleteOneVar;

%PUT ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;
%outcome;
%PUT ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;

*Create permanent dataset;
%let LABEL= walk; /*labels for outcomes*/
data savedata.BICsim_uncorr_&label; set BICsim_&label; proc sort; by sim; run;
proc delete data=BICsim_&label; run; quit;
***************************************************** END BACKWARD ELIMINATION 4 INDIVIDUAL OUTCOMES: WALK ******************************************************************;


*****************************************************PREPARE DATA FOR MACROs ********************************************************;
proc sort data=savedata.sim500uncorr out=simdata; by newid; run; 

*Merge with original dataset to get the covariates;
data simdata2; 
  merge savedata.originaldata (drop=time_adldepdth status_adldepdth time_iadldifdth status_iadldifdth time_walkdepdth status_walkdepdth time2death death) 
        simdata;
  by newid;
proc sort; by sim outcome newid; run;
/*5531*SIM=500->2,765,500*4outcomes=11,062,000 observations and 56+6-1=61 variables.*/

*Death;
data simdata3;
 set simdata2 (rename=(time=time2death status=death));
 where outcome="death";
proc sort; by sim newid; run; /*2,765,500 observations and 60 variables*/

proc delete data=simdata simdata2; run; quit;


**************************************************** DEFINE ARGUMENTS FOR MACROS: outcome, best_bic, DeleteOneVar, cstat ********************************************************;

*Note: all the macro variables for outcomes need to be in the same order, e.g. : adl iadl walk death; 
%let DATA=simdata4;
%let NUMOUTCOMES=1; /*number of outcomes*/
%let ALLOUTCOME=death;
%let ALLTIME=time2death;
%let ALLLABEL= death; /*labels for outcomes*/
%let NUMPRED=37; /*number of predictors without including predictors that are forced in (e.g. dAGE, SEX*/
%let NUMPREDPLUSONE=38; /*number of predictors defined in &NUMPRED (e.g.37) plus one*/
proc sql noprint; select max(sim) format 3. into :S from simdata3; quit; /*create macro variable with total number of simulated datasets*/
%put "&S"; 

***************************************************** BACKWARD ELIMINATION 4 INDIVIDUAL OUTCOMES: DEATH **************************************************************;
/*Macro outcome, for each simulated dataset and each outcome: 
1-) Calls macro BEST_BIC (calls macro DeleteOneVar)
2-) Creates one dataset with results from backward elimination
3-) Create a dataset with one line uncorresponding to the best model for each individual outcome and each simulation
*/

%macro outcome;
 %do i=1 %to &S; /*do this for each simulated dataset up to the last simulated dataset*/
  proc sql noprint; select (&i-1)*(nobs/&S)+1 into :fobs from dictionary.tables where libname='WORK' and memname='SIMDATA3'; quit; /*create macro variable with the first id of ith simulation*/
  proc sql noprint; select &i*(nobs/&S) into :lobs from dictionary.tables where libname='WORK' and memname='SIMDATA3'; quit; /*create macro variable with the last id of ith simulation*/

  data simdata4;
   set simdata3 (FIRSTOBS=&fobs OBS=&lobs);
  run;

  sasfile WORK.simdata4 load;

  %PUT "&i" ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;
  %do j=1 %to &NUMOUTCOMES;
    %let OUTCOME=%scan(&ALLOUTCOME,&j); /*extract the ith outcome, ith time, ith label*/
    %let TIME=%scan(&ALLTIME,&j);
	%let LABEL=%scan(&ALLLABEL,&j);
  
	%best_bic;

    sasfile WORK.BIC_&label close;

	/*Create new BIC_&label dataset to merge with other outcomes*/
	data BIC_&label (rename=(VARINMODEL=VARINMODEL_&label )); 
     set BIC_&label (drop=DELELIST DELEVAR);
     sim=&i;
    run;

	/*For each outcome and each simulation: select the best individual model*/
	proc sort data=BIC_&label; by descending BIC_&label; 
    data BIC_&label;
     set BIC_&label point=nobs nobs=nobs; 
	 output;
	 stop;
    run;

  %end; /*j loop*/

  sasfile WORK.simdata4 close;
  proc delete data=simdata4; run; quit;

  proc append base=BICsim_&label data=BIC_&label force; run; /*BICsim will have the information from all best individual models for each outcome and for each simulated dataset*/
  proc delete data=BIC_&label; run; quit;
  %PUT "&i" ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;

 %end; /*i loop*/
%mend outcome;

%macro best_bic;
	%let BASE=%sysfunc(compbl(dAGE SEX ALCOHOL ARTHRITIS CANCER COGDLRC3G COGIMRC3G DIABETES DRIVE EDU EXERCISE EYE2G
			                                   FALL HEARAID HEARING HEARTFAILURE HYPERTENSION INCONTINENCE LALONE LUNG MSTAT OTHERARM
	                                           OTHERCHAIR OTHERCLIM3G OTHERDIME OTHERLIFT OTHERPUSH OTHERSIT OTHERSTOOP OTHERWALK PAIN CESDALL
			                                   qBMI qFAGE qMAGE SHLT SMOKING STROKE VOLUNTEER)); /*all 39 initial variables*/
	%let DELE=%sysfunc(compbl(ALCOHOL ARTHRITIS CANCER COGDLRC3G COGIMRC3G DIABETES DRIVE EDU EXERCISE EYE2G
			                                   FALL HEARAID HEARING HEARTFAILURE HYPERTENSION INCONTINENCE LALONE LUNG MSTAT OTHERARM
	                                           OTHERCHAIR OTHERCLIM3G OTHERDIME OTHERLIFT OTHERPUSH OTHERSIT OTHERSTOOP OTHERWALK PAIN CESDALL
			                                   qBMI qFAGE qMAGE SHLT SMOKING STROKE VOLUNTEER)); /*all 39 variables-2(AGECAT SEX)=37*/

    proc phreg data = &DATA CONCORDANCE=HARRELL rocoptions(method=RECURSIVE iauc); 
      class &BASE;
      model &time*&outcome(0) = &BASE;
	  ods output FITSTATISTICS=FITS1 CONCORDANCE=concord IAUC=iauc;
    run;

	data _null_; set concord; call symputx ('c' ,Estimate); run;
	data _null_; set iauc; call symputx ('iauc' ,Estimate); run;

	data base_&label (keep=VARINMODEL DELEVAR DELELIST  AIC_&label BIC_&label C_&label iauc_&label); 
      length VARINMODEL $ 600 DELELIST $600 DELEVAR $ 60;
      set FITS1 end=last;
      retain VARINMODEL DELEVAR  C_&label iauc_&label AIC_&label BIC_&label; 
      format AIC_&label BIC_&label 10.4; 
	  VARINMODEL="&BASE";
	  DELEVAR=" ";
	  DELELIST=" "; 
	  C_&label=&c;
	  iauc_&label=&iauc;
	  if CRITERION='AIC' then AIC_&label=WITHCOVARIATES;
	  if CRITERION='SBC' then BIC_&label=WITHCOVARIATES;
      if last;
     run;
	
    proc delete data=FITS1 concord iauc; run; quit;

   proc append base=BIC_&label data=base_&label force; run; /*BIC is the final dataset with backward elimination steps from 39 variables to 2*/
   sasfile WORK.BIC_&label load;
   proc delete data=base_&label; run; quit;

	%do k=1 %to &NUMPRED;
	  %DeleteOneVar;
      data _null_;
       set CTABLE_&label (keep=VARINMODEL DELELIST );
       call symputx ('BASE' ,VARINMODEL);
	   call symputx ('DELE' ,DELELIST);
      run;

      proc append base=BIC_&label data=CTABLE_&label force; run;
	  proc delete data=CTABLE_&label; run; quit;
	%end;
%mend best_bic;

%macro DeleteOneVar;
 	%do l=1 %to %eval(&NUMPREDPLUSONE-&k);
     %let DELEVAR=%scan(&DELE,&l); /*select the lth word to delete. &DELE is defined in 'best_bic' macro*/
	 %let VARNAME=%sysfunc(compbl(%sysfunc(tranwrd(&BASE,&DELEVAR,%str( ))))); /*select the final set of variables to run model by replacing the deleted variable with blank*/

     proc phreg data = &DATA CONCORDANCE=HARRELL rocoptions(method=RECURSIVE iauc); 
      class &VARNAME;
      model &time*&outcome(0) = &VARNAME;
	  ods output FITSTATISTICS=FITS1 CONCORDANCE=concord IAUC=iauc;
     run;

	data _null_; set concord; call symputx ('c' ,Estimate); run;
	data _null_; set iauc; call symputx ('iauc' ,Estimate); run;

	data FITS2 (keep=VARINMODEL DELEVAR DELELIST   AIC_&label BIC_&label C_&label iauc_&label); 
      length VARINMODEL $ 600 DELELIST $600 DELEVAR $ 60;
      set FITS1 end=last;
      retain VARINMODEL DELEVAR  C_&label iauc_&label AIC_&label BIC_&label; 
      format AIC_&label BIC_&label 10.4; 
	  VARINMODEL="&VARNAME"; /*variables in the model*/
	  DELEVAR="&DELEVAR"; /*deleted variable*/
	  DELELIST=compbl(tranwrd("&DELE","&DELEVAR",' ')); /*'DELELIST' contain the list of variables that we need to start with in subsequent run.*/
	  C_&label=&c;
	  iauc_&label=&iauc;
	  if CRITERION='AIC' then AIC_&label=WITHCOVARIATES;
	  if CRITERION='SBC' then BIC_&label=WITHCOVARIATES;
      if last;
     run;

    %if &l=1 %then %do; /*in the 1st line of CTABLE_&label create CTABLE_&label dataset*/
     proc append base=CTABLE_&label data=FITS2 force; run;
     sasfile WORK.CTABLE_&label load;
    %end;
    %else %do; /*for the rest of lines of CTABLE_&label: keep updating CTABLE_&label dataset in memory*/
     proc append base=CTABLE_&label data=FITS2 force; run;
    %end; 
    proc delete data=FITS1 FITS2 concord iauc; run; quit;
  %end;
  sasfile WORK.CTABLE_&label close;

  proc sort data=CTABLE_&label; by descending BIC_&label; run;
  data CTABLE_&label;
   set CTABLE_&label point=nobs nobs=nobs;
   output;
   stop;
  run; /*choose the last observation with minimum BIC*/
%mend DeleteOneVar;

%PUT ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;
%outcome;
%PUT ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;

*Create permanent dataset;
%let LABEL= death; /*labels for outcomes*/
data savedata.BICsim_uncorr_&label; set BICsim_&label; proc sort; by sim; run;
proc delete data=BICsim_&label; run; quit;
***************************************************** END BACKWARD ELIMINATION 4 INDIVIDUAL OUTCOMES: DEATH ******************************************************************;



/***********************************************************************************************************************************************************************************/
/***********************************************************************************************************************************************************************************/
/*Get Union Model and compute summary statistics*/

*****************************************************PREPARE DATA FOR MACRO ********************************************************;
proc sort data=savedata.sim500uncorr out=simdata; by newid; run; 

*Merge with original dataset to get the covariates;
data simdata2; 
  merge savedata.originaldata (drop=time_adldepdth status_adldepdth time_iadldifdth status_iadldifdth time_walkdepdth status_walkdepdth time2death death) 
        simdata;
  by newid;
proc sort; by sim outcome newid; run;
/*11,062,000 observations and 55+5+1=61 variables.*/

/*Change format of simulated dataset from long where outcomes are stacked to wide where outcomes are merged*/
*ADL;
data adl;
 set simdata2 (rename=(time=time_adldepdth status=status_adldepdth));
 where outcome="adl";
proc sort; by sim newid; run; 
*IADL;
data iadl (keep=sim newid time_iadldifdth status_iadldifdth);
 set simdata2 (rename=(time=time_iadldifdth status=status_iadldifdth));
 where outcome="iadl";
proc sort; by sim newid; run; 
*Walk;
data walk (keep=sim newid time_walkdepdth status_walkdepdth);
 set simdata2 (rename=(time=time_walkdepdth status=status_walkdepdth));
 where outcome="walk";
proc sort; by sim newid; run; 
*Death;
data death (keep=sim newid time2death death);
 set simdata2 (rename=(time=time2death status=death));
 where outcome="death";
proc sort; by sim newid; run; 

data simdata3;
 merge adl iadl walk death;
 by sim newid;
proc sort; by sim newid; run; 

proc delete data=simdata simdata2 adl iadl walk death; run; quit;

*Define macro variables;
%let NUMOUTCOMES=4; /*number of outcomes*/
%let ALLOUTCOME=status_adldepdth status_iadldifdth status_walkdepdth death;
%let ALLTIME=time_adldepdth time_iadldifdth time_walkdepdth time2death;
%let ALLLABEL= adl iadl walk death; /*labels for outcomes*/
proc sql noprint; select max(sim) format 3. into :S from simdata3; quit; /*create macro variable with total number of simulated datasets*/
%put "&S"; 

/***********************************************************************************************************************************************/
*Create union of the best individual models for each outcome in each simulation;

/*Merge all 4 datasets with the best individual model within each outcome for each simulated dataset*/
data BICsim;
   merge savedata.BICsim_uncorr_adl savedata.BICsim_uncorr_iadl savedata.BICsim_uncorr_walk savedata.BICsim_uncorr_death;
   by sim;
run;

data savedata.BICnewsim&S.uncorrBy (drop=delims i variable allvars);
  set BICsim;
  length allvars $1000 variable jointvars $1000;
  allvars=compbl(catx(" ", VARINMODEL_adl, VARINMODEL_iadl, VARINMODEL_walk, VARINMODEL_death)); 
  /*catx: concatenate (join) two or more character strings, stripping both leading and trailing blanks and inserting blank separator between the strings*/
  delims = ' ';                 /*delimiter: space*/
  do i=1 to countw(allvars, delims); /*countw: for line of text, how many words/variables?*/
    variable=scan(allvars,i,delims); /*extract "words" from a string*/
    if indexw(jointvars,variable) then continue; /*CONTINUE statement stops the processing of the current iteration of a loop and resumes with the next iteration*/
	/*INDEXW(character-value, find-string): The function returns the first position in the character-value that contains the find-string. If the find-string is not found, the function returns a 0.*/
    jointvars=compbl(catx(" ",jointvars,variable));
  end;
  numVarsfinsim=countw(jointvars, delims);
  numVarsfinsimadl=countw(VARINMODEL_adl, delims);
  numVarsfinsimiadl=countw(VARINMODEL_iadl, delims);
  numVarsfinsimwalk=countw(VARINMODEL_walk, delims);
  numVarsfinsimdeath=countw(VARINMODEL_death, delims);
run;
/*proc print data=savedata.BICnewsim&S.uncorrBy (obs=3); var jointvars; run;*/

*Summary stats of simulated models by outcome;
proc means data=savedata.BICnewsim&S.uncorrBy stackodsoutput n mean clm stderr maxdec=4; 
 var numVarsfinsim numVarsfinsimadl numVarsfinsimiadl numVarsfinsimwalk numVarsfinsimdeath C_adl C_iadl C_walk C_death iauc_adl iauc_iadl iauc_walk iauc_death ;
 ods output summary=savedata.BICnewsim&S.uncorrBygralstats(rename=(Mean=MeanSimulationByOutcome));
proc sort; by variable; run;

PROC EXPORT DATA= savedata.BICnewsim&S.uncorrBygralstats
            OUTFILE= "path\BICnewsim&S.uncorrBygralstats.csv" 
            DBMS=CSV REPLACE;
     PUTNAMES=YES;
RUN;

***************************************************** Calculate C-stat for each outcome using the union models from simulated datasets ******************************************************************;
%macro cstat;
 %do i=1 %to &S;
 /*For each simulated dataset define VARNAME as the jointvars*/
  data _null_;
   set savedata.BICnewsim&S.uncorrBy (keep=sim jointvars);
   where sim=&i;
   call symputx ('VARNAME' ,jointvars);
  run;

  proc sql noprint; select (&i-1)*(nobs/&S)+1 into :fobs from dictionary.tables where libname='WORK' and memname='SIMDATA3'; quit; /*create macro variable with the first id of ith simulation*/
  proc sql noprint; select &i*(nobs/&S) into :lobs from dictionary.tables where libname='WORK' and memname='SIMDATA3'; quit; /*create macro variable with the last id of ith simulation*/

  data simdata4;
   set simdata3 (FIRSTOBS=&fobs OBS=&lobs);
  run;
  sasfile WORK.simdata4 load;

  %do j=1 %to &NUMOUTCOMES;
    %let OUTCOME=%scan(&ALLOUTCOME,&j); /*extract the jth outcome, jth time, jth label*/
    %let TIME=%scan(&ALLTIME,&j);
	%let LABEL=%scan(&ALLLABEL,&j);

	proc phreg data = simdata4 CONCORDANCE=HARRELL; 
      class &VARNAME;
      model &time*&outcome(0) = &VARNAME;
	  ods output CONCORDANCE=concord ;
    run;

	data CTABLE_&label;
     set concord (keep= estimate rename=(estimate=c_&label));
	 length VARINMODEL $1000;
     sim=&i;
	 VARINMODEL="&VARNAME";
    run;

	proc delete data=concord; run; quit;

  %end; /*j loop*/

  sasfile WORK.simdata4 close;
  proc delete data=simdata4; run; quit;

  data ctable; 
    merge CTABLE_adl CTABLE_iadl(drop=VARINMODEL) CTABLE_walk(drop=VARINMODEL) CTABLE_death(drop=VARINMODEL);
	by sim;
    C_avg=mean(c_adl,c_iadl, c_walk, c_death);
  run;

  proc delete data=CTABLE_adl CTABLE_iadl CTABLE_walk CTABLE_death; run; quit;

  proc append base=ctablesim data=ctable force; run;
  proc delete data=ctable; run; quit;

  %end; /*i loop*/
%mend cstat;

%PUT ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;
%cstat;
%PUT ======MONITORING: %SYSFUNC(DATE(),YYMMDD10.), %LEFT(%SYSFUNC(TIME(),HHMM8.))======;

proc sort data=ctablesim out=savedata.BICnewsim&S.uncorrByUnionC; by sim; run;
proc delete data=ctablesim; run; quit;

PROC EXPORT DATA= savedata.BICnewsim&S.uncorrByUnionC
            OUTFILE= "path\BICnewsim&S.uncorrByUnionC.csv" 
            DBMS=CSV REPLACE;
     PUTNAMES=YES;
RUN;

*Summary stats of simulated models by outcome;
proc means data=savedata.BICnewsim&S.uncorrByUnionC stackodsoutput n mean clm stderr maxdec=4; 
 var C_adl C_iadl C_walk C_death C_avg;
 ods output summary=savedata.BICnewsim&S.uncorrByUnionCmeans(rename=(Mean=MeanSimByOutcomeUnionModel));
proc sort; by variable; run;

PROC EXPORT DATA= savedata.BICnewsim&S.uncorrByUnionCmeans
            OUTFILE= "path\BICnewsim&S.uncorrByUnionCmeans.csv" 
            DBMS=CSV REPLACE;
     PUTNAMES=YES;
RUN;


proc delete data=simdata3; run; quit;




